<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Siemens TIA PLC控制面板模拟器</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Microsoft YaHei", Arial, sans-serif;
        }
        
        body {
            background-color: #f0f0f0;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            display: flex;
            flex-direction: column;
            max-width: 1500px;
            margin: 0 auto;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            min-height: 95vh;
        }
        
        .panel-container {
            background-color: #2c3e50;
            padding: 25px;
            color: white;
            order: 1;
            flex: 1;
        }
        
        .display-container {
            padding: 25px;
            background-color: #ecf0f1;
            display: flex;
            flex-direction: column;
            order: 2;
            flex: 1;
        }
        
        .panel-title {
            text-align: center;
            margin-bottom: 25px;
            color: #3498db;
            font-weight: bold;
            font-size: 24px;
        }
        
        .channels-container {
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            gap: 25px;
            margin-bottom: 25px;
        }
        
        .channel {
            background-color: #34495e;
            border-radius: 8px;
            padding: 20px;
            display: flex;
            flex-direction: column;
        }
        
        .channel-title {
            text-align: center;
            margin-bottom: 20px;
            color: #f39c12;
            font-size: 16px;
            font-weight: bold;
        }
        
        .button-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 12px;
            flex: 1;
        }
        
        .square-button {
            background-color: #2c3e50;
            border: none;
            color: white;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            transition: background-color 0.2s;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            aspect-ratio: 1/1;
            min-height: 60px;
        }
        
        .square-button:hover {
            background-color: #3d566e;
        }
        
        .square-button:active {
            transform: none;
        }
        
        .square-button.active {
            background-color: #3498db;
        }
        
        .light-button {
            position: relative;
        }
        
        .light-button::after {
            content: "";
            position: absolute;
            width: 8px;
            height: 8px;
            background-color: #7f8c8d;
            border-radius: 50%;
            top: 5px;
            right: 5px;
        }
        
        .light-button.active::after {
            background-color: #e74c3c;
            box-shadow: 0 0 5px #e74c3c;
        }
        
        /* 特殊按钮样式 - 点击后颜色变暗并加粗框线 */
        .reset-button {
            background-color: #e74c3c;
            border: none;
            color: white;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 60px;
            margin-top: 12px;
            font-weight: bold;
            border: 2px solid transparent;
        }
        
        .reset-button:hover {
            background-color: #c0392b;
        }
        
        .reset-button.active {
            background-color: #a93226;
            border: 3px solid #ff8c7a;
            box-shadow: 0 0 8px rgba(231, 76, 60, 0.6);
        }
        
        .circular-button {
            background-color: #2c3e50;
            border: none;
            color: white;
            border-radius: 50%;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            aspect-ratio: 1/1;
            min-height: 100px;
            margin-bottom: 20px;
            border: 2px solid transparent;
        }
        
        .circular-button:hover {
            background-color: #3d566e;
        }
        
        .circular-button.active {
            background-color: #1a2530;
            border: 3px solid #3498db;
            box-shadow: 0 0 8px rgba(52, 152, 219, 0.6);
        }
        
        .start-stop-container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
        }
        
        .start-button {
            background-color: #27ae60;
            border: none;
            color: white;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            transition: all 0.2s;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 50px;
            border: 2px solid transparent;
        }
        
        .stop-button {
            background-color: #e74c3c;
            border: none;
            color: white;
            border-radius: 6px;
            cursor: pointer;
            font-size: 12px;
            transition: all 0.2s;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 50px;
            border: 2px solid transparent;
        }
        
        .start-button:hover {
            background-color: #219653;
        }
        
        .stop-button:hover {
            background-color: #c0392b;
        }
        
        .start-button.active {
            background-color: #1e8449;
            border: 3px solid #58d68d;
            box-shadow: 0 0 8px rgba(39, 174, 96, 0.6);
        }
        
        .stop-button.active {
            background-color: #a93226;
            border: 3px solid #ff8c7a;
            box-shadow: 0 0 8px rgba(231, 76, 60, 0.6);
        }
        
        .display-area {
            flex: 1;
            background-color: white;
            border: 1px solid #bdc3c7;
            border-radius: 4px;
            padding: 20px;
            overflow-y: auto;
            margin-top: 15px;
            min-height: 350px;
        }
        
        .display-title {
            color: #2c3e50;
            margin-bottom: 10px;
            font-weight: bold;
        }
        
        .button-info {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: none;
        }
        
        .button-name {
            font-weight: bold;
            color: #e74c3c;
            margin-bottom: 5px;
            font-size: 18px;
        }
        
        .button-description {
            color: #2c3e50;
            line-height: 1.6;
            font-size: 16px;
        }
        
        .no-selection {
            color: #7f8c8d;
            text-align: center;
            margin-top: 30px;
            font-style: italic;
            font-size: 16px;
        }
        
        .file-upload {
            margin-bottom: 15px;
            padding: 15px;
            background-color: #d5dbdb;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .file-upload label {
            font-weight: bold;
            color: #2c3e50;
            margin-right: 15px;
            font-size: 16px;
            white-space: nowrap;
        }
        
        .file-input-container {
            display: flex;
            align-items: center;
            flex: 1;
            justify-content: flex-end;
        }
        
        .status-message {
            padding: 8px 12px;
            border-radius: 4px;
            font-size: 14px;
            margin-right: 15px;
            white-space: nowrap;
            max-width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        
        .status-success {
            background-color: #d4efdf;
            color: #27ae60;
            border: 1px solid #27ae60;
        }
        
        .status-error {
            background-color: #fadbd8;
            color: #e74c3c;
            border: 1px solid #e74c3c;
        }
        
        /* 响应式设计 */
        @media (max-width: 1400px) {
            .channels-container {
                grid-template-columns: repeat(3, 1fr);
                gap: 20px;
            }
        }
        
        @media (max-width: 1024px) {
            .channels-container {
                grid-template-columns: repeat(2, 1fr);
            }
            
            .square-button {
                min-height: 55px;
            }
        }
        
        @media (max-width: 768px) {
            .channels-container {
                grid-template-columns: 1fr;
                gap: 15px;
            }
            
            .panel-container, .display-container {
                padding: 20px;
            }
            
            .file-upload {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .file-input-container {
                margin-top: 10px;
                width: 100%;
                justify-content: flex-start;
            }
            
            .status-message {
                margin-right: 0;
                margin-bottom: 2px;
                max-width: 50%;
            }
        }
        
        @media (max-width: 480px) {
            body {
                padding: 10px;
            }
            
            .container {
                min-height: auto;
            }
            
            .panel-container, .display-container {
                padding: 15px;
            }
            
            .square-button {
                min-height: 50px;
                font-size: 11px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 控制面板 -->
        <div class="panel-container">
            <h2 class="panel-title">Siemens TIA PLC 控制面板</h2>
            
            <!-- 5栏布局 -->
            <div class="channels-container">
                <!-- 第一栏 -->
                <div class="channel">
                    <div class="channel-title">操作模式</div>
                    <div class="button-grid">
                        <button class="square-button" data-id="JOG">JOG</button>
                        <button class="square-button" data-id="REPOS">REPOS</button>
                        <button class="square-button" data-id="REF POINT">REF POINT</button>
                        <button class="square-button" data-id="TEACH IN">TEACH IN</button>
                        <button class="square-button" data-id="VAR">VAR</button>
                        <button class="square-button" data-id="1">1</button>
                        <button class="square-button" data-id="MDA">MDA</button>
                        <button class="square-button" data-id="10">10</button>
                        <button class="square-button" data-id="100">100</button>
                        <button class="square-button" data-id="AUTO">AUTO</button>
                        <button class="square-button" data-id="1000">1000</button>
                        <button class="square-button" data-id="10000">10000</button>
                        <button class="square-button" data-id="SINGLE BLOCK">SINGLE BLOCK</button>
                        <button class="square-button" data-id="CYCLE STOP">CYCLE STOP</button>
                        <button class="square-button" data-id="CYCLE START">CYCLE START</button>
                    </div>
                    <button class="reset-button" data-id="RESET">RESET</button>
                </div>
                
                <!-- 第二栏 -->
                <div class="channel">
                    <div class="channel-title">功能指示灯</div>
                    <div class="button-grid">
                        <button class="square-button light-button active" data-id="T1">T1<br>一档</button>
                        <button class="square-button light-button" data-id="T2">T2<br>二档</button>
                        <button class="square-button light-button" data-id="T3">T3<br>三档</button>
                        <button class="square-button light-button" data-id="T4">T4<br>超程解除</button>
                        <button class="square-button light-button" data-id="T5">T5<br>U轴有效</button>
                        <button class="square-button light-button" data-id="T6">T6<br>主轴松刀</button>
                        <button class="square-button light-button" data-id="T7">T7<br>刀具外冷</button>
                        <button class="square-button light-button" data-id="T8">T8<br>刀具内冷</button>
                        <button class="square-button light-button" data-id="T9">T9</button>
                        <button class="square-button light-button" data-id="T10">T10<br>排屑反转</button>
                        <button class="square-button light-button" data-id="T11">T11</button>
                        <button class="square-button light-button" data-id="T12">T12<br>转台定位</button>
                        <button class="square-button light-button" data-id="T13">T13<br>排屑正转</button>
                        <button class="square-button light-button" data-id="T14">T14<br>机床照明</button>
                        <button class="square-button light-button" data-id="T15">T15<br>液压启动</button>
                    </div>
                </div>
                
                <!-- 第三栏 -->
                <div class="channel">
                    <div class="channel-title">轴控制</div>
                    <div class="button-grid">
                        <button class="square-button" data-id="R1">R1<br>X</button>
                        <button class="square-button" data-id="R2">R2<br>Y</button>
                        <button class="square-button" data-id="R3">R3<br>Z</button>
                        <button class="square-button" data-id="R4">R4<br>W</button>
                        <button class="square-button" data-id="R5">R5<br>B</button>
                        <button class="square-button" data-id="R6">R6<br>V</button>
                        <button class="square-button" data-id="R7">R7<br>U</button>
                        <button class="square-button" data-id="R8">R8<br>SP</button>
                        <button class="square-button" data-id="R9">R9<br>C</button>
                        <button class="square-button" data-id="R10">R10<br>A</button>
                        <button class="square-button" data-id="R11">R11<br>TA</button>
                        <button class="square-button" data-id="R12">R12<br>WCS MCS</button>
                        <button class="square-button" data-id="-">-</button>
                        <button class="square-button" data-id="RAPID">RAPID</button>
                        <button class="square-button" data-id="+">+</button>
                    </div>
                </div>
                
                <!-- 第四栏 -->
                <div class="channel">
                    <div class="channel-title">主轴控制</div>
                    <button class="circular-button" data-id="主轴百分比">主轴<br>百分比</button>
                    <div class="start-stop-container">
                        <button class="stop-button" data-id="SPINDLE STOP">SPINDLE<br>STOP</button>
                        <button class="start-button" data-id="SPINDLE START">SPINDLE<br>START</button>
                    </div>
                </div>
                
                <!-- 第五栏 -->
                <div class="channel">
                    <div class="channel-title">进给控制</div>
                    <button class="circular-button" data-id="供给轴百分比">供给轴<br>百分比</button>
                    <div class="start-stop-container">
                        <button class="stop-button" data-id="FEED STOP">FEED<br>STOP</button>
                        <button class="start-button" data-id="FEED START">FEED<br>START</button>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 显示区域 -->
        <div class="display-container">
            <!-- Excel文件上传区域 -->
            <div class="file-upload">
                <label for="excel-file">上传Excel文件（第一列为按钮名，第二列为说明）</label>
                <div class="file-input-container">
                    <div id="upload-status" class="status-message"></div>
                    <input type="file" id="excel-file" accept=".xlsx, .xls">
                </div>
            </div>
            
            <div class="display-area" id="displayArea">
                <div class="no-selection">请上传Excel文件并点击按钮查看功能说明</div>
            </div>
        </div>
    </div>

    <script>
        // 全局变量存储Excel数据
        let buttonData = {};
        
        // 获取DOM元素
        const excelFileInput = document.getElementById('excel-file');
        const uploadStatus = document.getElementById('upload-status');
        const buttons = document.querySelectorAll('.square-button, .reset-button, .circular-button, .start-button, .stop-button');
        const displayArea = document.getElementById('displayArea');
        
        // 处理Excel文件上传
        excelFileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (!file) return;
            
            const reader = new FileReader();
            
            reader.onload = function(e) {
                try {
                    const data = new Uint8Array(e.target.result);
                    const workbook = XLSX.read(data, { type: 'array' });
                    
                    // 获取第一个工作表
                    const firstSheetName = workbook.SheetNames[0];
                    const worksheet = workbook.Sheets[firstSheetName];
                    
                    // 将工作表转换为JSON
                    const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
                    
                    // 处理数据：第一列为按钮名，第二列为说明
                    buttonData = {};
                    jsonData.forEach(row => {
                        if (row.length >= 2 && row[0] && row[1]) {
                            buttonData[row[0]] = {
                                name: row[0],
                                description: row[1]
                            };
                        }
                    });
                    
                    // 显示成功消息
                    uploadStatus.textContent = `成功加载 ${Object.keys(buttonData).length} 个按钮数据`;
                    uploadStatus.className = 'status-message status-success';
                    
                    // 更新显示区域提示
                    displayArea.innerHTML = '<div class="no-selection">请点击按钮查看功能说明</div>';
                    
                } catch (error) {
                    console.error('Excel文件解析错误:', error);
                    uploadStatus.textContent = '文件解析错误，请确保文件格式正确';
                    uploadStatus.className = 'status-message status-error';
                }
            };
            
            reader.onerror = function() {
                uploadStatus.textContent = '文件读取失败';
                uploadStatus.className = 'status-message status-error';
            };
            
            reader.readAsArrayBuffer(file);
        });
        
        // 为每个按钮添加点击事件
        buttons.forEach(button => {
            button.addEventListener('click', function() {
                const buttonId = this.getAttribute('data-id');
                
                // 移除所有按钮的active类
                buttons.forEach(btn => btn.classList.remove('active'));
                
                // 为当前点击的按钮添加active类
                this.classList.add('active');
                
                // 检查是否已加载Excel数据
                if (Object.keys(buttonData).length === 0) {
                    displayArea.innerHTML = '<div class="no-selection">请先上传Excel文件</div>';
                    return;
                }
                
                // 显示对应的说明
                if (buttonData[buttonId]) {
                    displayArea.innerHTML = `
                        <div class="button-info">
                            <div class="button-name">${buttonData[buttonId].name}</div>
                            <div class="button-description">${buttonData[buttonId].description}</div>
                        </div>
                    `;
                } else {
                    displayArea.innerHTML = `<div class="no-selection">未找到"${buttonId}"按钮的说明</div>`;
                }
            });
        });
        
        // 初始化：添加默认数据（如果没有上传Excel文件时使用）
        function initializeDefaultData() {
            // 这里可以添加一些默认数据，但实际应用中应该从Excel加载
            buttonData = {};
        }
        
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', initializeDefaultData);
    </script>
</body>
</html>